package com.liner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liner.vo.BuyerOrderDetailVo;
import com.liner.vo.BuyerOrderMasterVo;
import com.liner.entity.OrderDetail;
import com.liner.entity.OrderMaster;
import com.liner.entity.ProductInfo;
import com.liner.feign.ProductFeign;
import com.liner.form.BuyerOrderDetailForm;
import com.liner.form.BuyerOrderForm;
import com.liner.mapper.OrderDetailMapper;
import com.liner.mapper.OrderMasterMapper;
import com.liner.service.OrderMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author liner
 * @since 2022-12-03
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {


    @Autowired
    private ProductFeign productFeign;

    @Autowired
    WebSocket webSocket;

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    @Transactional
    public String create(BuyerOrderForm buyerOrderForm) {
        //先存主表
        //TODO 重写BeanUtils     BeanUtils.copyProperties(buyerOrderForm,BuyerOrderMasterVo.class);
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setBuyerName(buyerOrderForm.getName());
        orderMaster.setBuyerPhone(buyerOrderForm.getPhone());
        orderMaster.setBuyerAddress(buyerOrderForm.getAddress());
        orderMaster.setBuyerOpenid(buyerOrderForm.getId());
        List<BuyerOrderDetailForm> items = buyerOrderForm.getItems();
        BigDecimal orderAmount = new BigDecimal(0);
        for (BuyerOrderDetailForm item : items) {
            //通过id查询商品价格
            BigDecimal price = productFeign.findPriceById(item.getProductId());
            BigDecimal amount = price.multiply(new BigDecimal(item.getProductQuantity()));
            orderAmount = orderAmount.add(amount);
        }
        orderMaster.setOrderAmount(orderAmount);
        orderMaster.setOrderStatus(0);
        orderMaster.setPayStatus(0);
        orderMasterMapper.insert(orderMaster);
        //再存从表
        for (BuyerOrderDetailForm item : items) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderMaster.getOrderId());
            orderDetail.setProductId(item.getProductId());
            orderDetail.setProductQuantity(item.getProductQuantity());
            ProductInfo byId = productFeign.findById(item.getProductId());
            BeanUtils.copyProperties(byId, orderDetail);
            orderDetailMapper.insert(orderDetail);

            //减库存
            productFeign.subStockById(item.getProductId(), item.getProductQuantity());
        }

        //通知后台管理系统
        webSocket.sendMessage("有新的订单");
        return orderMaster.getOrderId();
    }

    @Override
    public BuyerOrderMasterVo detail(Integer buyerId, String orderId) {
        //先查询主表
        LambdaQueryWrapper<OrderMaster> orderMasterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderMasterLambdaQueryWrapper.eq(OrderMaster::getOrderId, orderId);
        orderMasterLambdaQueryWrapper.eq(OrderMaster::getBuyerOpenid, buyerId);
        OrderMaster orderMaster = orderMasterMapper.selectOne(orderMasterLambdaQueryWrapper);
        //封装到 BuyerOrderMasterVo
        BuyerOrderMasterVo buyerOrderMasterVo = new BuyerOrderMasterVo();
        BeanUtils.copyProperties(orderMaster, buyerOrderMasterVo);
        //再查询从表
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orderId);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
        List<BuyerOrderDetailVo> buyerOrderDetailVoList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            //封装到   BuyerOrderDetailVo
            BuyerOrderDetailVo buyerOrderDetailVo = new BuyerOrderDetailVo();
            BeanUtils.copyProperties(orderDetail, buyerOrderDetailVo);
            buyerOrderDetailVoList.add(buyerOrderDetailVo);
        }
        buyerOrderMasterVo.setOrderDetailList(buyerOrderDetailVoList);
        return buyerOrderMasterVo;
    }

    @Override
    @Transactional
    public Boolean cancel(Integer buyerId, String orderId) {
        //获得对应订单的 数量
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orderId);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
        for (OrderDetail orderDetail : orderDetailList) {
            //加库存
            productFeign.addStockById(orderDetail.getProductId(), orderDetail.getProductQuantity());
        }
        if (buyerId == null) {
            return orderMasterMapper.SellerCancel(orderId);
        }else {
            return orderMasterMapper.cancel(buyerId, orderId);
        }
    }

    @Override
    public Boolean finish(String orderId) {
        return orderMasterMapper.finish(orderId);
    }

    @Override
    public Boolean pay(Integer buyerId, String orderId) {
        return orderMasterMapper.pay(buyerId, orderId);
    }

}
